package com.zxh.blog.web.security.service.impl;

import com.zxh.blog.utils.ValidationUtil;
import com.zxh.blog.web.security.entity.SysUser;
import com.zxh.blog.web.security.mapper.SysUserMapper;
import com.zxh.blog.web.security.service.SysUserService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户(SysUser)表服务实现类
 *
 * @author zxh
 * @since 2019-02-16 15:00:54
 */
@Service("sysUserService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUser queryById(String userId) {
        return this.sysUserMapper.queryById(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<SysUser> queryAllByLimit(int offset, int limit) {
        return this.sysUserMapper.queryAllByLimit(offset, limit);
    }

    @Override
    public List<SysUser> queryAll(SysUser sysUser) {
        return this.sysUserMapper.queryAll(sysUser);
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser insert(SysUser sysUser) {
        this.sysUserMapper.insert(sysUser);
        return sysUser;
    }

    /**
     * 修改数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Override
    public SysUser update(SysUser sysUser) {
        this.sysUserMapper.update(sysUser);
        return this.queryById(sysUser.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String userId) {
        return this.sysUserMapper.deleteById(userId) > 0;
    }

    /**
     * 注册
     *
     * @param sysUser  user对象
     * @return 是否成功
     */
    @Override
    public String register(SysUser sysUser) {
        String msg="";
        try {
           int i= sysUserMapper.insert(sysUser);
            msg=i==1?"注册成功！！":"注册失败！！";
            if(i==1){
                amqpTemplate.convertAndSend("register",sysUser);
            }
        }catch (Exception e){
            msg="注册失败！！";
        }
        return msg;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user=new SysUser();
        List<SysUser>sysUsers=new ArrayList<SysUser>();
        //判断是不是邮箱
        if(ValidationUtil.isEmail(username)){
            user.setEmail(username);
            sysUsers=sysUserMapper.queryAll(user);
        }
        //邮箱找不到 通过username 查询
        if(sysUsers.size()==0){
            user.setUsername(username);
            sysUsers=sysUserMapper.queryAll(user);
        }
        return sysUsers.size()>0?sysUsers.get(0):null;
    }
}